package club.zhcs.captrue.utils;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpSession;

import club.zhcs.capture.bean.auth.User;
import club.zhcs.capture.vo.Consts;

import com.dgj.utils.collection.Lists;

import org.apache.velocity.tools.config.DefaultKey;
import org.apache.velocity.tools.config.InvalidScope;
import org.nutz.mvc.Mvcs;


/**
 * @author 贵源 <br>
 *         create at 2014年10月21日
 */
@DefaultKey("S")
@InvalidScope({ "application" })
public class SessionUtils {

	public static Object get(String key) {
		return getSession().getAttribute(key);
	}

	public static User getLoginUser() {
		Object obj = getSession().getAttribute(Consts.SessionKeys.SESSION_USER_KEY);
		if (obj != null) {
			return (User) obj;
		}
		return null;
	}

	/**
	 * 获取session
	 * 
	 * @return
	 */
	private static HttpSession getSession() {
		return Mvcs.getReq().getSession();
	}

	public static String getTest() {
		return "test";
	}

	@SuppressWarnings("unchecked")
	public static boolean hasRole(String roleName) {
		List<String> nil = Lists.newArrayList();
		List<String> roles = (get("roles") == null ? nil : (List<String>) get("roles"));
		Collections.sort(roles);
		return Collections.binarySearch(roles, roleName) >= 0;
	}

}
